<?php
/**
 * The world of warcraft armory library was created by Marc Binder. The called
 * product will be deployed by Tof Technologies, Germany. All sources contain to 
 * the New BSD Licence. Self created extensions can be contained in the hall of 
 * fame. So the community has the chance to get a overview of all parsers and
 * handlers, which are really effective.
 * 
 * Get more informations:
 * http://code.google.com/p/warcraftarmory/
 * 
 * @author      Marc Binder <marcandrebinder@gmail.com>
 * @copyright   (c) Tof Technologies, Germany - 2010
 * @license     New BSD Licence
 * @package     Wow_Armory
 * @version     $Id$
 */

/**
 * Wow_Armory_Core_Curl
 * 
 * @category    Wow_Armory
 * @package     Core
 * @subpackage  Curl
 * @example     n/A
 */
class Wow_Armory_Core_Curl
{
    
    /**
     * Includes the curl object.
     * 
     * @var curl|object
     */
    protected $_cUrl = null;
    
    /**
     * Creates the object.
     * 
     * @return void
     */
    public function __construct()
    { }
    
    /**
     * Inits the curl class.
     * 
     * @return Wow_Armory_Core_Curl
     */
    public function init()
    {
        $this->_cUrl = curl_init();
        return $this;
    }
    
    /**
     * Sets the url.
     * 
     * @param  string $url
     * @return Wow_Armory_Core_Curl
     */
    public function setUrl($url)
    {
        if (!is_string($url)
            || empty($url)
        ) {
            throw new Wow_Armory_Exception('', 1015);
        }
        
        curl_setopt($this->_cUrl, CURLOPT_URL, $url);
        return $this;
    }
    
    /**
     * Sets the user agent.
     * 
     * @param  string $agent
     * @return Wow_Armory_Core_Curl
     */
    public function setUserAgent($agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12")
    {
        if (!is_string($agent)
            || empty($agent)
        ) {
            throw new Wow_Armory_Exception('', 1016);
        }
        
        curl_setopt(
            $this->_cUrl, 
            CURLOPT_USERAGENT, 
            $agent
        );
        return $this;
    }
    
    /**
     * Execute the curl method.
     * 
     * @return string
     */
    public function execute()
    {
        curl_setopt($this->_cUrl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($this->_cUrl);
        curl_close($this->_cUrl);
        return $result;
    }
    
}